package com.kylin.module.exhibition.business;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

import com.kylin.common.Constants;
import com.kylin.common.dict.orm.Dict;
import com.kylin.common.exception.BusinessException;
import com.kylin.common.util.FileOperater;
import com.kylin.hibernate.BaseHibernateDAO;
import com.kylin.module.exhibition.forms.ExhibitionForm;
import com.kylin.module.exhibition.orm.Exhibition;

public class ExhibitionBlogic {

	protected static Logger log = Logger.getLogger(ExhibitionBlogic.class);
	
	public void add(ExhibitionForm form) throws BusinessException
	{
		log.debug("开始添加会展信息"+form.getTitle());
		Exhibition obj = new Exhibition();
		obj.setCatExhibition(new Dict(form.getCatExhibitionId()));
		obj.setTitle(form.getTitle());
		obj.setContent(form.getContent());
		obj.setCountry(form.getCountry());
		obj.setCreatedon(new Date());
		obj.setLanguage(form.getLanguage());
		if(form.getStartTime() != null && !form.getStartTime().equals(""))
		{
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			try{
				obj.setStartTime(format.parse(form.getStartTime()));
		
			}catch(ParseException e){
				throw new BusinessException("日期格式错误");
			}
		}
		if(form.getEndTime() != null && !form.getEndTime().equals(""))
		{
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			try{
				obj.setEndTime(format.parse(form.getEndTime()));
		
			}catch(ParseException e){
				throw new BusinessException("日期格式错误");
			}
		}
		if(form.getImagePath() != null && form.getImagePath().getFileSize() > 0)
		{
			log.debug("文件大小为："+form.getImagePath().getFileSize());
			String fileName = form.getImagePath().getFileName();
			String imgUrl = null;
			try
			{
				imgUrl = FileOperater.saveUploadFile(Constants.IMG_EXHIBITION,form.getImagePath().getInputStream(),fileName);
			}catch(IOException e)
			{
				log.error(e);
				e.printStackTrace();
				throw new BusinessException("上传图片文件失败",e);
			}
			obj.setImagePath(imgUrl);
		}
		BaseHibernateDAO.save(obj);
	}
	
	public Exhibition getById(Long id) throws BusinessException
	{
		return (Exhibition)BaseHibernateDAO.findById(Exhibition.class, id);
	}
	
	public void deleteById(Long id) throws BusinessException
	{
		Exhibition obj = getById(id);
		if(obj != null)
		{
			BaseHibernateDAO.deleteById(Exhibition.class,id);
		}
	}
	
	public void update(ExhibitionForm form) throws BusinessException
	{
		log.debug("更新会展信息的ID:"+form.getId());
		Exhibition obj = getById(form.getId());
		if(form.getCatExhibitionId().intValue() != 0)
		{
			obj.setCatExhibition(new Dict(form.getCatExhibitionId()));
		}
		obj.setContent(form.getContent());
		obj.setCountry(form.getCountry());
		obj.setLanguage(form.getLanguage());
		obj.setLastupdatedon(new Date());
		obj.setTitle(form.getTitle());
		if (form.getImagePath() != null && form.getImagePath().getFileSize() > 0
			&& form.getImagePath().getFileName() != null && !form.getImagePath().getFileName().equals("")) {
			String fileName = form.getImagePath().getFileName();
			String imgUrl = null;
			try {
				imgUrl = FileOperater.updateUploadFile(Constants.IMG_EXHIBITION,form.getImagePath().getInputStream(), fileName, obj.getImagePath());
				obj.setImagePath(imgUrl);
			} catch (IOException e) {
				log.error(e);
				e.printStackTrace();
				throw new BusinessException("上传文件图片失败",e);
			}
		}
		if(form.getStartTime() != null && !form.getStartTime().equals(""))
		{
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			try{
				obj.setStartTime(format.parse(form.getStartTime()));
		
			}catch(ParseException e){
				throw new BusinessException("日期格式错误");
			}
		}
		if(form.getEndTime() != null && !form.getEndTime().equals(""))
		{
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			try{
				obj.setEndTime(format.parse(form.getEndTime()));
		
			}catch(ParseException e){
				throw new BusinessException("日期格式错误");
			}
		}
		BaseHibernateDAO.update(obj);
	}
}
